﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.UserCommandHandler
{
    /// <summary>
    /// 反填用户单表数据命令
    /// </summary>
    public class GetFanTianUserCommandHandler : IRequestHandler<GetFanTianUserCommand, APIResult<User>>
    {
        private readonly IBaseRepository<User> userRepos;

        public GetFanTianUserCommandHandler(IBaseRepository<User> userRepos)
        {
            this.userRepos = userRepos;
        }
        /// <summary>
        /// 反填用户单表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<User>> Handle(GetFanTianUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<User> result = new APIResult<User>();
            var obj = userRepos.GetAll().Result.FirstOrDefault(x => x.UserId == request.UserId);
            if (obj == null)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            else
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = obj;
            }

            return Task.FromResult(result);

        }
    }
}
